package com.molichuxing.gateway.api.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseCodeEnum;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.UploadUtil;
import com.molichuxing.gateway.api.services.BasicApiService;
import com.molichuxing.gateway.api.services.GoodsApiService;
import com.molichuxing.gateway.bff.basic.entity.response.FileStorageConfigVo;
import com.molichuxing.gateway.bff.basic.entity.response.ReceiptCompanyVo;
import com.molichuxing.gateway.bff.basic.service.FileStorageBffService;
import com.molichuxing.gateway.bff.basic.service.ReceiptCompanyBffService;
import com.molichuxing.gateway.bff.goods.response.GoodsBaseVo;
import com.molichuxing.gateway.bff.goods.response.GoodsDetailVo;
import com.molichuxing.gateway.bff.goods.response.GoodsVo;
import com.molichuxing.gateway.bff.goods.service.GoodsBffService;
import com.molichuxing.gateway.utils.RequestAttrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 商品信息
 * @author xuelin yi
 */
@RestController
@RequestMapping(value = "/goods")
@Api(tags="商品信息")
public class GoodsApiServiceImpl extends RootApiService implements GoodsApiService {
	private static final Logger logger = LoggerFactory.getLogger(GoodsApiServiceImpl.class);

	@Resource
	private GoodsBffService goodsBffService;

	@Override
	@ApiOperation(value = "获取商品列表信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "goodsType", value = "商品类型", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "code", value = "商品编码", required = false, dataType = "String"),
			@ApiImplicitParam(name = "carStyleId", value = "车型id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/list")
	public ResponseResult<Paged<GoodsVo>> getGoodsList(@RequestParam Map<String, Object> selectParams, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
		ResponseResult<Paged<GoodsVo>> result = new ResponseResult<>();
		try {
			//获取当前登录的经销商id
			Integer dealerId = RequestAttrUtil.getCurrentDealerId();
			selectParams.put("dealerId", dealerId);
			result.success(goodsBffService.getGoodsList(selectParams, pageNum, pageSize));
		} catch (BizException e) {
			logger.error("[list]获取商品列表信息异常", e);
			result.error(e.getExceptionCode(), e.getMessage());
		} catch (Exception e) {
			logger.error("[list]获取商品列表信息异常", e);
			result.error("获取商品列表信息异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "获取商品基础信息")
	@ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Integer")
	@GetMapping(value = "/base")
	public ResponseResult<GoodsBaseVo> getBaseGoods(Integer id) {
		ResponseResult<GoodsBaseVo> result = new ResponseResult<>();
		try {
			return result.success(goodsBffService.getBaseGoods(id));
		} catch (Exception e) {
			logger.error("[baseGoods]获取商品基础信息异常", e);
			result.error("获取商品基础信息异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "获取商品详细信息")
	@ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Integer")
	@GetMapping(value = "/detail")
	public ResponseResult<GoodsDetailVo> getDetailGoods(Integer id) {
		ResponseResult<GoodsDetailVo> result = new ResponseResult<>();
		try {
			return result.success(goodsBffService.getDetailGoods(id));
		} catch (Exception e) {
			logger.error("[baseGoods]获取商品详细信息异常", e);
			result.error("获取商品详细信息异常");
		}
		return result;
	}
}
